package com.ruoyi.util;

import com.google.common.io.Files;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * TODO
 *
 * @author chenchuan2
 * @date 2023-01-13 16:09:45
 */
public class FileUtil {

    public static String getFileName(String path) {
        int i = path.lastIndexOf("/");
        return path.substring(i+1);
    }

    public static List<String> readFile(String path) {
        File file = new File(path);

        List<String> lines = Lists.newArrayList();
        try {
            lines = Files.readLines(file, StandardCharsets.UTF_8);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }

    public static List<FileEmbedding> convert(List<String> result){
        List<FileEmbedding> list = Lists.newArrayList();
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<result.size();i++){
            if(StringUtils.isBlank(result.get(i))){
                String context = sb.toString();;
                sb = new StringBuilder();
                FileEmbedding fileEmbedding = new FileEmbedding();
                fileEmbedding.setContent(context);
                fileEmbedding.setHashCode(context.hashCode());
                list.add(fileEmbedding);
            }else{
                sb.append(result.get(i));
            }
        }
        return list;
    }

    public static void main(String[] args) {
        String fileName = getFileName("/Users/chenchuan/Downloads/chatgpt/kaoqin.txt");
        System.out.println(fileName);
    }
}
